package com.smedi.ismedi.constructionproject.core.infrastructure.converter;

import com.github.dozermapper.core.CustomConverter;
import com.github.dozermapper.core.MappingException;
import com.smedi.ismedi.constructionproject.core.domain.aggregatemodel.BidSectionConfigOrganization;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: xulinjun
 * @Date: 2021-6-22 14:30
 */
public class BidSectionConfigOrganizationConverter implements CustomConverter {
    @Override
    public Object convert(Object existingDestinationFieldValue, Object sourceFieldValue, Class<?> destinationClass, Class<?> sourceClass) {
        if (sourceFieldValue == null) {
            return new ArrayList();
        }
        if (sourceFieldValue instanceof List) {
            List<Object> destinations = new ArrayList<>();
            List<?> list = (List<?>) sourceFieldValue;
            if (list.size() > 0) {
                if (list.get(0) instanceof Long) {
                    for (Object object : list) {
                        BidSectionConfigOrganization organization = new BidSectionConfigOrganization().setProjectOrganizationId((Long) object);
                        destinations.add(organization);
                    }
                    return destinations;
                } else if (list.get(0) instanceof BidSectionConfigOrganization) {
                    for (Object object : list) {
                        destinations.add(((BidSectionConfigOrganization)object).getProjectOrganizationId());
                    }
                }
                return destinations;
            } else {
                return new ArrayList();
            }
        } else {
            throw new MappingException("Converter BidSectionConfigOrganizationConverter Error: "
                    + existingDestinationFieldValue
                    + " and "
                    + destinationClass);
        }
    }
}
